<template>
  <div class="statistics-container">
    <div class="statistics-header">
      <h1>面试统计</h1>
      <p>查看面试数据和趋势分析</p>
    </div>
    
    <!-- 统计概览 -->
    <div class="overview-section">
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><Document /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ totalInterviews }}</div>
          <div class="stat-label">总面试数</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><User /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ totalCandidates }}</div>
          <div class="stat-label">候选人数</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><Check /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ passRate }}%</div>
          <div class="stat-label">通过率</div>
        </div>
      </div>
      
      <div class="stat-card">
        <div class="stat-icon">
          <el-icon><Clock /></el-icon>
        </div>
        <div class="stat-content">
          <div class="stat-number">{{ averageDuration }}分钟</div>
          <div class="stat-label">平均时长</div>
        </div>
      </div>
    </div>
    
    <!-- 面试列表 -->
    <div class="interviews-section">
      <div class="section-header">
        <h3>面试列表</h3>
        <div class="header-actions">
          <el-input 
            v-model="searchQuery" 
            placeholder="搜索面试..." 
            prefix-icon="Search"
            style="width: 200px;"
          />
          <el-select v-model="statusFilter" placeholder="筛选状态" style="width: 120px;">
            <el-option label="全部" value="" />
            <el-option label="进行中" value="ongoing" />
            <el-option label="已完成" value="completed" />
            <el-option label="已暂停" value="paused" />
          </el-select>
        </div>
      </div>
      
      <div class="interviews-table">
        <el-table :data="filteredInterviews" style="width: 100%">
          <el-table-column prop="name" label="面试名称" width="200">
            <template #default="{ row }">
              <div class="interview-name">
                <strong>{{ row.name }}</strong>
                <div class="interview-position">{{ row.position }}</div>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column prop="status" label="状态" width="100">
            <template #default="{ row }">
              <el-tag :type="getStatusTagType(row.status)">
                {{ getStatusText(row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          
          <el-table-column prop="totalCandidates" label="候选人数" width="100" />
          
          <el-table-column prop="completedCandidates" label="已完成" width="100" />
          
          <el-table-column label="完成率" width="120">
            <template #default="{ row }">
              <div class="completion-rate">
                <el-progress 
                  :percentage="getCompletionRate(row)" 
                  :stroke-width="6"
                  :show-text="false"
                />
                <span>{{ getCompletionRate(row) }}%</span>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column prop="passedCandidates" label="通过人数" width="100" />
          
          <el-table-column label="录用率" width="120">
            <template #default="{ row }">
              <div class="pass-rate">
                <el-progress 
                  :percentage="getPassRate(row)" 
                  :stroke-width="6"
                  :show-text="false"
                  :color="getPassRateColor(row)"
                />
                <span>{{ getPassRate(row) }}%</span>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column prop="createdAt" label="创建时间" width="120">
            <template #default="{ row }">
              {{ formatDate(row.createdAt) }}
            </template>
          </el-table-column>
          
          <el-table-column label="操作" width="150">
            <template #default="{ row }">
              <div class="table-actions">
                <el-button type="text" @click="viewDetails(row)">
                  详情
                </el-button>
                <el-button type="text" @click="viewCandidates(row)">
                  候选人
                </el-button>
                <el-button type="text" @click="editInterview(row)">
                  编辑
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    
    <!-- 数据分析 -->
    <div class="analysis-section">
      <div class="analysis-grid">
        <!-- 通过率趋势 -->
        <div class="analysis-card">
          <h4>通过率趋势</h4>
          <div class="chart-container">
            <div class="trend-chart">
              <div class="chart-line">
                <div 
                  v-for="(point, index) in trendData.passRate" 
                  :key="index"
                  class="trend-point"
                  :style="{ 
                    left: `${(index / (trendData.passRate.length - 1)) * 100}%`,
                    bottom: `${point}%`
                  }"
                  :title="`${trendData.months[index]}: ${point}%`"
                ></div>
              </div>
              <div class="chart-labels">
                <span 
                  v-for="(month, index) in trendData.months" 
                  :key="index"
                  class="chart-label"
                  :style="{ left: `${(index / (trendData.months.length - 1)) * 100}%` }"
                >
                  {{ month }}
                </span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 面试数量趋势 -->
        <div class="analysis-card">
          <h4>面试数量趋势</h4>
          <div class="chart-container">
            <div class="bar-chart">
              <div 
                v-for="(count, index) in trendData.interviewCount" 
                :key="index"
                class="bar-item"
                :style="{ height: `${(count / Math.max(...trendData.interviewCount)) * 100}%` }"
                :title="`${trendData.months[index]}: ${count}场`"
              ></div>
            </div>
            <div class="chart-labels">
              <span 
                v-for="(month, index) in trendData.months" 
                :key="index"
                class="chart-label"
              >
                {{ month }}
              </span>
            </div>
          </div>
        </div>
        
        <!-- 岗位分布 -->
        <div class="analysis-card">
          <h4>岗位分布</h4>
          <div class="position-distribution">
            <div 
              v-for="position in positionStats" 
              :key="position.name"
              class="position-item"
            >
              <div class="position-info">
                <span class="position-name">{{ position.name }}</span>
                <span class="position-count">{{ position.count }}人</span>
              </div>
              <div class="position-progress">
                <el-progress 
                  :percentage="(position.count / totalCandidates) * 100" 
                  :stroke-width="8"
                  :show-text="false"
                  :color="position.color"
                />
              </div>
            </div>
          </div>
        </div>
        
        <!-- 能力评估分布 -->
        <div class="analysis-card">
          <h4>能力评估分布</h4>
          <div class="ability-distribution">
            <div class="ability-radar">
              <div class="radar-pentagon">
                <div 
                  v-for="(ability, index) in abilityStats" 
                  :key="ability.name"
                  class="ability-point"
                  :style="getRadarPointStyle(ability, index)"
                  :title="`${ability.name}: ${ability.average}分`"
                ></div>
              </div>
              <div class="radar-labels">
                <span 
                  v-for="(ability, index) in abilityStats" 
                  :key="ability.name"
                  class="radar-label"
                  :style="getRadarLabelStyle(index)"
                >
                  {{ ability.name }}
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 详细统计 -->
    <div class="detailed-stats">
      <h3>详细统计</h3>
      <div class="stats-grid">
        <div class="stat-item">
          <div class="stat-title">平均面试时长</div>
          <div class="stat-value">{{ averageDuration }} 分钟</div>
          <div class="stat-change positive">
            <el-icon><ArrowUp /></el-icon>
            <span>比上月增长 5%</span>
          </div>
        </div>
        
        <div class="stat-item">
          <div class="stat-title">候选人满意度</div>
          <div class="stat-value">4.2/5.0</div>
          <div class="stat-change positive">
            <el-icon><ArrowUp /></el-icon>
            <span>比上月提升 0.3</span>
          </div>
        </div>
        
        <div class="stat-item">
          <div class="stat-title">面试官效率</div>
          <div class="stat-value">85%</div>
          <div class="stat-change negative">
            <el-icon><ArrowDown /></el-icon>
            <span>比上月下降 2%</span>
          </div>
        </div>
        
        <div class="stat-item">
          <div class="stat-title">系统稳定性</div>
          <div class="stat-value">99.8%</div>
          <div class="stat-change positive">
            <el-icon><ArrowUp /></el-icon>
            <span>比上月提升 0.1%</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Document, User, Check, Clock, ArrowUp, ArrowDown } from '@element-plus/icons-vue'

export default {
  name: 'StatisticsView',
  components: {
    Document,
    User,
    Check,
    Clock,
    ArrowUp,
    ArrowDown
  },
  setup() {
    const router = useRouter()
    
    const searchQuery = ref('')
    const statusFilter = ref('')
    
    // 总体统计数据
    const totalInterviews = ref(48)
    const totalCandidates = ref(156)
    const passRate = ref(68)
    const averageDuration = ref(42)
    
    // 面试列表数据
    const interviews = ref([
      {
        id: 1,
        name: '前端开发工程师面试',
        position: '前端开发工程师',
        status: 'ongoing',
        totalCandidates: 25,
        completedCandidates: 18,
        passedCandidates: 12,
        createdAt: '2024-01-15'
      },
      {
        id: 2,
        name: '后端开发工程师面试',
        position: '后端开发工程师',
        status: 'completed',
        totalCandidates: 30,
        completedCandidates: 30,
        passedCandidates: 20,
        createdAt: '2024-01-10'
      },
      {
        id: 3,
        name: '产品经理面试',
        position: '产品经理',
        status: 'ongoing',
        totalCandidates: 15,
        completedCandidates: 8,
        passedCandidates: 5,
        createdAt: '2024-01-20'
      },
      {
        id: 4,
        name: 'UI设计师面试',
        position: 'UI设计师',
        status: 'paused',
        totalCandidates: 12,
        completedCandidates: 3,
        passedCandidates: 2,
        createdAt: '2024-01-18'
      }
    ])
    
    // 趋势数据
    const trendData = reactive({
      months: ['1月', '2月', '3月', '4月', '5月', '6月'],
      passRate: [62, 58, 65, 72, 68, 75],
      interviewCount: [15, 18, 22, 28, 24, 32]
    })
    
    // 岗位统计
    const positionStats = ref([
      { name: '前端开发', count: 45, color: '#409EFF' },
      { name: '后端开发', count: 52, color: '#67C23A' },
      { name: '产品经理', count: 28, color: '#E6A23C' },
      { name: 'UI设计师', count: 21, color: '#F56C6C' },
      { name: '数据分析师', count: 10, color: '#909399' }
    ])
    
    // 能力评估统计
    const abilityStats = ref([
      { name: '逻辑思维', average: 78 },
      { name: '专业技能', average: 82 },
      { name: '沟通表达', average: 75 },
      { name: '团队协作', average: 80 },
      { name: '学习适应', average: 85 }
    ])
    
    const filteredInterviews = computed(() => {
      let filtered = interviews.value
      
      if (searchQuery.value) {
        filtered = filtered.filter(interview => 
          interview.name.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
          interview.position.toLowerCase().includes(searchQuery.value.toLowerCase())
        )
      }
      
      if (statusFilter.value) {
        filtered = filtered.filter(interview => interview.status === statusFilter.value)
      }
      
      return filtered
    })
    
    const formatDate = (date) => {
      return new Date(date).toLocaleDateString('zh-CN')
    }
    
    const getStatusTagType = (status) => {
      const typeMap = {
        ongoing: 'primary',
        completed: 'success',
        paused: 'warning'
      }
      return typeMap[status] || 'default'
    }
    
    const getStatusText = (status) => {
      const textMap = {
        ongoing: '进行中',
        completed: '已完成',
        paused: '已暂停'
      }
      return textMap[status] || '未知'
    }
    
    const getCompletionRate = (interview) => {
      if (interview.totalCandidates === 0) return 0
      return Math.round((interview.completedCandidates / interview.totalCandidates) * 100)
    }
    
    const getPassRate = (interview) => {
      if (interview.completedCandidates === 0) return 0
      return Math.round((interview.passedCandidates / interview.completedCandidates) * 100)
    }
    
    const getPassRateColor = (interview) => {
      const rate = getPassRate(interview)
      if (rate >= 70) return '#67C23A'
      if (rate >= 50) return '#E6A23C'
      return '#F56C6C'
    }
    
    const getRadarPointStyle = (ability, index) => {
      const angle = (index * 72 - 90) * Math.PI / 180
      const radius = 80
      const x = Math.cos(angle) * radius * (ability.average / 100)
      const y = Math.sin(angle) * radius * (ability.average / 100)
      
      return {
        left: `calc(50% + ${x}px)`,
        top: `calc(50% + ${y}px)`
      }
    }
    
    const getRadarLabelStyle = (index) => {
      const angle = (index * 72 - 90) * Math.PI / 180
      const radius = 100
      const x = Math.cos(angle) * radius
      const y = Math.sin(angle) * radius
      
      return {
        left: `calc(50% + ${x}px)`,
        top: `calc(50% + ${y}px)`
      }
    }
    
    const viewDetails = (interview) => {
      // 跳转到面试详情页面
      router.push(`/hr/interview/${interview.id}`)
    }
    
    const viewCandidates = (interview) => {
      // 跳转到候选人列表页面
      router.push(`/hr/candidates?interview=${interview.id}`)
    }
    
    const editInterview = (interview) => {
      // 跳转到编辑面试页面
      router.push(`/hr/interview/${interview.id}/edit`)
    }
    
    return {
      searchQuery,
      statusFilter,
      totalInterviews,
      totalCandidates,
      passRate,
      averageDuration,
      interviews,
      trendData,
      positionStats,
      abilityStats,
      filteredInterviews,
      formatDate,
      getStatusTagType,
      getStatusText,
      getCompletionRate,
      getPassRate,
      getPassRateColor,
      getRadarPointStyle,
      getRadarLabelStyle,
      viewDetails,
      viewCandidates,
      editInterview
    }
  }
}
</script>

<style scoped>
.statistics-container {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
  background: #f8f9fa;
}

.statistics-header {
  margin-bottom: 32px;
  text-align: center;
}

.statistics-header h1 {
  color: #333;
  margin-bottom: 8px;
}

.statistics-header p {
  color: #666;
  margin: 0;
}

.overview-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 32px;
}

.stat-card {
  background: white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 16px;
}

.stat-icon {
  width: 48px;
  height: 48px;
  background: #f0f2f5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #6753C0;
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.interviews-section,
.analysis-section,
.detailed-stats {
  background: white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 24px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h3 {
  color: #333;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.interview-name {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.interview-position {
  font-size: 12px;
  color: #666;
}

.completion-rate,
.pass-rate {
  display: flex;
  align-items: center;
  gap: 8px;
}

.completion-rate span,
.pass-rate span {
  font-size: 12px;
  color: #666;
}

.table-actions {
  display: flex;
  gap: 8px;
}

.analysis-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.analysis-card {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 20px;
}

.analysis-card h4 {
  color: #333;
  margin-bottom: 16px;
}

.chart-container {
  height: 200px;
  position: relative;
}

.trend-chart {
  position: relative;
  height: 150px;
  border-bottom: 1px solid #e4e7ed;
}

.chart-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.trend-point {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #6753C0;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
}

.chart-labels {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 30px;
  display: flex;
  align-items: center;
}

.chart-label {
  position: absolute;
  font-size: 12px;
  color: #666;
  transform: translateX(-50%);
}

.bar-chart {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  height: 150px;
  padding: 0 20px;
}

.bar-item {
  flex: 1;
  background: #6753C0;
  border-radius: 4px 4px 0 0;
  min-height: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.bar-item:hover {
  background: #5a4baa;
}

.position-distribution {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.position-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.position-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-width: 120px;
}

.position-name {
  font-size: 14px;
  color: #333;
}

.position-count {
  font-size: 12px;
  color: #666;
}

.position-progress {
  flex: 1;
}

.ability-distribution {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.ability-radar {
  position: relative;
  width: 180px;
  height: 180px;
}

.radar-pentagon {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: 2px solid #e4e7ed;
  border-radius: 50%;
}

.ability-point {
  position: absolute;
  width: 6px;
  height: 6px;
  background: #6753C0;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
}

.radar-labels {
  position: absolute;
  top: -20px;
  left: -20px;
  width: calc(100% + 40px);
  height: calc(100% + 40px);
}

.radar-label {
  position: absolute;
  font-size: 12px;
  color: #666;
  transform: translate(-50%, -50%);
}

.detailed-stats h3 {
  color: #333;
  margin-bottom: 20px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.stat-item {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
}

.stat-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.stat-change {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 12px;
}

.stat-change.positive {
  color: #67C23A;
}

.stat-change.negative {
  color: #F56C6C;
}
</style> 